import { createRouter, createWebHistory } from "vue-router";
import { showFailToast } from 'vant'

import Home from '../views/home/index.vue'


const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
        meta: { is_footer: true, is_header: true }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login/index.vue'),
        meta: { is_footer: false, is_header: true }
    },
    {
        path: '/self',
        name: 'self',
        component: () => import('../views/self/index.vue'),
        meta: { is_footer: false, is_header: false }
    },
    {
        path: '/cate',
        name: 'cate',
        component: () => import('../views/cate/index.vue'),
        meta: { is_footer: true, is_header: true }
    },
    {
        path: '/cart',
        name: 'cart',
        component: () => import('../views/cart/index.vue'),
        meta: { is_footer: true, is_header: true }
    },
    {
        path: '/list',
        name: 'list',
        component: () => import('../views/list/index.vue'),
        meta: { is_footer: false, is_header: true }
    },
    {
        path: '/detail',
        name: 'detail',
        component: () => import('../views/detail/index.vue'),
        meta: { is_footer: false, is_header: false }
    },
    {
        path: '/search',
        name: 'search',
        component: () => import('../views/search/index.vue'),
        meta: { is_footer: false, is_header: true }
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../views/register/index.vue'),
        meta: { is_footer: false, is_header: true }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 准备一个数组,表示需要验证登录的路由
const list: Array<string> = ['/self']
router.beforeEach(to => {
    if (list.indexOf(to.fullPath) === -1) return true
    const token: string = window.localStorage.getItem('token') || ''
    if (token) return true
    // 提示
    showFailToast('您还没有登录')


    // 跳转到登录页
    setTimeout(() => {
        router.push('/login')

    }, 1000)
    return false
})

// 导出路由
export default router